﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PermissionDesign.Model;
using PermissionDesign.Model.Entity;
using PermissionDesign.Model.Request;
using PermissionDesign.Model.Response;
using PermissionDesign.Service;
using PermissionDesign.Tool;

namespace PermissionDesign.Web.Controllers
{
    public class UserController : Controller
    {
        private UserService _userService;
        private RoleService _roleService;
        private string superAdminEncoding = EnumHelper.SingleTon().GetEnumDescription(EnumConfig.SuperAdminEncoding);
        public UserController(UserService userService, RoleService roleService)
        {
            _userService = userService;
            _roleService = roleService;
        }

        public IActionResult Index()
        {
            //角色ID
            int roleId = int.Parse(CookieHelper.SingleTon().GetCookieValue(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingRoleIdKey), Request));
            //当前角色的拥有的功能编码
            ViewData["AuthorizeActions"] = _roleService.GetActionByRoleId(roleId);
            return View(_roleService.GetRoles(c => c.RoleEncoding != superAdminEncoding));
        }

        #region 分页查询用户信息
        /// <summary>
        /// 分页查询用户信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPageUserList(int pageIndex, int pageSize, string search_keyword, DateTime? startTime, DateTime? endTime, int search_state = 1)
        {
            var total = 0;
            var where = new List<Expression<Func<User, bool>>>();
            if (search_state != -1)
            {
                where.Add(c => c.IsEnabled == search_state);
            }
            if (!string.IsNullOrEmpty(search_keyword))
            {
                where.Add(c => c.Name.Contains(search_keyword));
            }
            if (startTime != null)
            {
                where.Add(c => c.RegisterTime >= startTime);
            }
            if (endTime != null)
            {
                where.Add(c => c.RegisterTime <= endTime);
            }

            var content = _userService.UserPageQuery(where, out total, pageIndex, pageSize);
            if (content.code == 200)
            {
                return Json(new { total = total, rows = content.data as List<Response_User> });
            }
            else
            {
                return Json(new { total = 0, rows = "" });
            }
        }
        #endregion

        #region 查询用户账号是否存在
        /// <summary>
        /// 查询用户账号是否存在
        /// </summary>
        /// <param name="loginAccount"></param>
        /// <returns></returns>
        public JsonResult IsLoginAccountExist(string loginAccount)
        {
            return Json(_userService.IsUserExist(c => c.LoginAccount == loginAccount));
        }
        #endregion

        #region 添加用户
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddUser(Request_AddUser user)
        {
            ResponseModel response = _userService.AddUser(user);
            if (response.code != 200)
            {
                Response.Redirect(@"/Wrong/Index?wrongContent=" + response.result + "&returnUrl=/User/Index");
                return null;
            }
            else
            {
                return View("Index", _roleService.GetRoles(c => c.RoleEncoding != superAdminEncoding));
            }
        }
        #endregion

        #region 编辑用户信息
        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditUser(Request_EditUser user)
        {
            ResponseModel response = _userService.EditUser(user);
            if (response.code != 200)
            {
                Response.Redirect(@"/Wrong/Index?wrongContent=" + response.result + "&returnUrl=/User/Index");
                return null;
            }
            else
            {
                return View("Index", _roleService.GetRoles(c => c.RoleEncoding != superAdminEncoding));
            }
        }

        #endregion

    }
}